const dialogs = document.querySelectorAll('.npc-dialog');
const avatars = document.querySelectorAll('.npc-avatar img');
const names = document.querySelectorAll('.npc-name p');
let currentDialogIndex = 0;

function showCurrentDialog() {
  if (currentDialogIndex < dialogs.length) {
    dialogs[currentDialogIndex].style.display = 'block';
    names[currentDialogIndex].style.display = 'block';
    // 显示当前对话对应的头像
    avatars[currentDialogIndex].classList.remove('hidden-avatar');
  } else {
    // 所有对话结束，跳转到新手村
    window.location.href = 'index3.html';;
  }
}

function closeCurrentDialog() {
  dialogs[currentDialogIndex].style.display = 'none';
  names[currentDialogIndex].style.display = 'none';
  avatars[currentDialogIndex].classList.add('hidden-avatar');
  currentDialogIndex++;
  showCurrentDialog();
}


// 页面加载完成后显示第一个对话
window.onload = showCurrentDialog;

// 绑定点击屏幕关闭对话框，并显示下一个对话
document.addEventListener('click', closeCurrentDialog);
/*背景音乐的播放*/
function playMusic() {
  var audio = document.getElementById("myAudio");
  audio.play();
}
  /*点击屏幕的音效*/
  var clickSound = new Audio('../music/click.mp3');

    function playClickSound() {
      clickSound.play();
    }

    document.addEventListener('click', function() {
      playClickSound();
    });
    /*特殊音效和窗口抖动*/
    var specialSound = new Audio('../music/fight.mp3');

    function playSpecialSoundAndShake() {
      specialSound.play();
      var dialogBox = document.querySelector('.dialog-box');
      dialogBox.classList.add('shake');
      setTimeout(function() {
        dialogBox.classList.remove('shake');
      }, 500);
    }
  /*关闭一些窗口后出现音效*/
  function closeCurrentDialogAndPlaySound() {
    // 关闭当前对话框
    closeCurrentDialog();
  
    // 播放特定音效
    playSpecialSound();
  }

  
  var specialSound = new Audio('../music/ao.mp3');

  function playSpecialSound() {
  specialSound.play();
}

 // 获取主角元素
 var character = document.getElementById('character');

 // 监听点击事件
 document.addEventListener('click', function(event) {
   // 如果点击的是背景图上的某个点，则移动主角到该位置
   if (event.target.tagName === 'HTML') {
     moveCharacter(event.clientX, event.clientY);
   }
 });

    
    // 获取全屏元素
var fullscreenElement = document.getElementById('fullscreen');

// 进入全屏模式
if (fullscreenElement.requestFullscreen) {
  fullscreenElement.requestFullscreen();
} else if (fullscreenElement.mozRequestFullScreen) {
  fullscreenElement.mozRequestFullScreen();
} else if (fullscreenElement.webkitRequestFullscreen) {
  fullscreenElement.webkitRequestFullscreen();
} else if (fullscreenElement.msRequestFullscreen) {
  fullscreenElement.msRequestFullscreen();
}
